package general.dbClass;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class DBThumbData {
	private int		nr;
	private byte[] 	data;
	private int 	size;
	
	public DBThumbData() {
		setNr(0);
		this.data 	= null;
		this.size	= 0;
	}
	
	public DBThumbData(int nr,String name) {
		setNr(nr);
		this.data 	= null;
		this.size	= 0;
	}
	
	public DBThumbData(int nr,String name,byte[] data) {
		this.setNr(nr);

		try {
			this.setData(data);
		}catch(IOException e) {
			this.data = null;
			this.size = 0;
			
			e.printStackTrace();
		}
	}

	/**
	 * @param data the data to set
	 * @throws IOException 
	 */
	public void setData(byte[] data) throws IOException {
		InputStream 			is 		= new ByteArrayInputStream(data);
		ByteArrayOutputStream 	os 		= new ByteArrayOutputStream();
		BufferedImage 			inBuf 	= ImageIO.read(is);
		Image 					tmp 	= inBuf.getScaledInstance(160, 120, Image.SCALE_SMOOTH);
		BufferedImage			retBuf 	= new BufferedImage(160,120,BufferedImage.TYPE_INT_RGB);
		Graphics 				g 		= retBuf.getGraphics();
		g.drawImage(tmp,0,0,null);
		
		ImageIO.write(retBuf, "jpg", os);
		
		os.flush();
		
		this.data = os.toByteArray();
		this.size = this.data.length;
		
		os.close();
	}

	/**
	 * @return the data
	 */
	public byte[] getData() {
		return data;
	}

	/**
	 * @param size the size to set
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * @return the size
	 */
	public int getSize() {
		return size;
	}

	/**
	 * @param nr the nr to set
	 */
	public void setNr(int nr) {
		this.nr = nr;
	}

	/**
	 * @return the nr
	 */
	public int getNr() {
		return nr;
	}	
}
